package Main.FileUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

    public static BufferedReader getBufferReader(File f) throws FileNotFoundException {
        return new BufferedReader(new InputStreamReader(new FileInputStream(f)));
    }

    public static BufferedWriter getBufferWriter(File f) throws FileNotFoundException {
        return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
    }

    public static List<String> readFile(File f) throws IOException {
        List<String> list = new ArrayList<>();

        BufferedReader bufferedReader = getBufferReader(f);

        String line;

        while((line = bufferedReader.readLine())!=null){
            list.add(line);
        }

       return list;
    }

    public static void writeToFile(File f, List<String > list) throws IOException {
        BufferedWriter bufferedWriter = getBufferWriter(f);

        for (int i=0;i<list.size()-1;i++){
            bufferedWriter.write(list.get(i));
            bufferedWriter.newLine();
        }
        bufferedWriter.write(list.get(list.size()-1));
        bufferedWriter.close();

    }

    public static List<String> filesToAbsPathList(String filePath){
        File file = new File(filePath);
        File[] files = file.listFiles();

        List<String> pathAndName = new ArrayList<>();
        for (File f : files){
            pathAndName.add(f.getAbsolutePath().replaceAll("\\\\","/"));
        }

        return pathAndName;
    }

    public static List<String> filesToPathList(String filePath){
        File file = new File(filePath);
        File[] files = file.listFiles();

        List<String> pathAndName = new ArrayList<>();
        for (File f : files){
            String name = f.getParentFile().getName() + "\\\\" + f.getName();
            pathAndName.add(name.replaceAll("\\\\","/"));
        }

        return pathAndName;
    }


}
